home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / fplib20 / fmod.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  1KB  |  65 lines

  1. /* CEIL, FLOOR, MODF, and FMOD functions for Sozobon C.            */
  2. /* Copyright © David Brooks, 1989 All Rights Reserved            */
  3.  
  4. #include <fplib.h>
  5.  
  6. /* This macro returns true if the number is so large that is has no    */
  7. /* representable fraction.                        */
  8.  
  9. #define ISLGINT(a)    (((a).sc[3] & EXP_MASK) > (BIAS + MANT_BITS))
  10.  
  11. /* CEIL: smallest integral value not less than a.            */
  12.  
  13. float ceil(a) 
  14. fstruct a;
  15. {    register float    aint;
  16.  
  17.     if (ISLGINT(a))
  18.         return a.f;
  19.  
  20.     if ((aint = (float)((long)a.f)) != a.f && a.sc[3] >= 0)
  21.         ++aint;
  22.  
  23.     return aint;
  24. }
  25.  
  26. /* FLOOR: largest integral value not greater than a.            */
  27.  
  28. float floor(a)
  29. fstruct a;
  30. {    register float    aint;
  31.  
  32.     if (ISLGINT(a))
  33.         return a.f;
  34.  
  35.     if ((aint = (float)((long)a.f)) != a.f && a.sc[3] < 0)
  36.         --aint;
  37.  
  38.     return aint;
  39. }
  40.  
  41. /* Just for once, we luck out: negative numbers behave as needed... */
  42.  
  43. float modf(a, ip)
  44. fstruct a;
  45. float *ip;
  46. {    long ipart;
  47.  
  48.     *ip = (ISLGINT(a))?a.f:(float)(long)a.f;
  49.  
  50.     return a.f - *ip;
  51. }
  52.  
  53. float fmod(n, d)
  54. float n, d;
  55. {    fstruct quot;
  56.     long iquot;
  57.  
  58.     quot.f = n / d;
  59.     if (ISLGINT(quot))
  60.         return 0.0;
  61.  
  62.     iquot = (long)quot.f;
  63.     return n - (float)iquot * d;
  64. }
  65.